package android.sms.auto.responder;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
//import android.security.MessageDigest;
import java.security.MessageDigest;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidSmsAutoResponder extends Activity {

	public static final String PASSWORD_PREF_KEY = "passwd";

	private TextView messages;
	private EditText pass1;
	private EditText pass2;
	public static String myLongitude;
	public static String myLatitude;
	@Override
	public void onCreate(Bundle icicle) {
		try{
		super.onCreate(icicle);
		setContentView(R.layout.main);
		messages = (TextView) findViewById(R.id.text1);
		pass1 = (EditText) findViewById(R.id.password);
		pass2 = (EditText) findViewById(R.id.password_confirm);

		Button button = (Button) findViewById(R.id.ok);
		button.setOnClickListener(clickListener);
		}
		catch(Exception e)
		{
			Toast.makeText(null, e.toString(), Toast.LENGTH_LONG);
		}
		
	}

	private OnClickListener clickListener = new OnClickListener() {

		public void onClick(View v) {
			String p1 = pass1.getText().toString();
			String p2 = pass2.getText().toString();

			if (p1.equals(p2)) {

				if (p1.length() >= 6 || p2.length() >= 6) {

					Editor passwdfile = getSharedPreferences(AndroidSmsAutoResponder.PASSWORD_PREF_KEY, 0).edit();
					String md5hash = getMd5Hash(p1);
					passwdfile.putString(AndroidSmsAutoResponder.PASSWORD_PREF_KEY,
							md5hash);
					passwdfile.commit();
					messages.setText("Password updated!");

				} else
					messages.setText("Passwords must be at least 6 characters");

			} else {
				pass1.setText("");
				pass2.setText("");
				messages.setText("Passwords do not match");
			}

		}

	};

	public static String getMd5Hash(String input) {
		try	{
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(input.getBytes());
			BigInteger number = new BigInteger(1,messageDigest);
			String md5 = number.toString(16);
	    	
			while (md5.length() < 32)
				md5 = "0" + md5;
	    	
			return md5;
		} catch(NoSuchAlgorithmException e) {
			Log.e("MD5", e.getMessage());
			return null;
		}
	}


	
}